/**
 * Created by zhaohenry on 2017/6/12.
 */
import * as types from './mutation-types'

export default {
  // 新增tab
  [types.ADD_TAB_LIST] (state, payload) {
    // 判断tabs存在 如果存在 直接切换
    if (state.activeList.indexOf(payload.activeName) + 1) {
      state.activeName = payload.activeName
    } else {
      state.tabsList.push(payload.tab)
      state.activeList.push(payload.activeName)
      state.activeName = payload.activeName
    }
  },
  // 删除tab
  [types.DEL_TAB_LIST] (state, activeName) {
    let idx = state.activeList.indexOf(activeName)
    if (activeName === state.activeName) {
      state.activeName = state.activeList[idx + 1] || state.activeList[idx - 1]
    }
    state.tabsList.splice(idx, 1)
    state.activeList.splice(idx, 1)
  },
  // 修改tab，主要刷新当前页 TODO：可能需要个返回功能
  [types.MODIFY_TAB_LIST] (state, payload) {
    let idx = state.activeList.indexOf(payload.activeName)
    if (idx + 1) {
      state.tabsList[idx] = payload.tab
    } else {
      // TODO: 新建页面？
    }
  },
  // 清空tab
  [types.CLEAR_TAB_LIST] (state) {
    state.tabsList.splice(0, state.tabsList.length)
    state.activeList.splice(0, state.activeList.length)
  },
  // 修改title
  [types.MODIFY_TITLE] (state, title) {
    state.spiderForm.title = title
  },
  // 修改auth
  [types.MODIFY_AUTH] (state, status) {
    state.auth = status
  }
}

